Editor.DeckEdit = function () {
	var fig;
	var back;
	var text;

	var impl;
	var shape;

	var detail;
	var detailText;
	var detailBack;

	return {
		init: function (s) {
			shape = s;
			impl = shape.getImplementation();
		},
		remove: function () {
			fig.remove();
			delete fig;
			text.remove();
			delete text;
			back.remove();
			delete back;
		},
		draw: function () {
			var x = shape.getX() + Editor.CONFIG.DECKEDIT_RADIUS - Editor.CONFIG.DECKEDIT_DELTA;
			var y = shape.getY() + Editor.CONFIG.DECKEDIT_DELTA - Editor.CONFIG.DECKEDIT_RADIUS + shape.getHeight();

			text = Editor.STATE.CANVAS.text(x, y, Editor.CONFIG.DECKEDIT_TEXTSYMBOL);
			text.attr(Editor.CONFIG.DECKEDIT_ATTR_TEXTSYMBOL);

			back = Editor.STATE.CANVAS.circle(x, y, Editor.CONFIG.DECKEDIT_RADIUS).attr(Editor.CONFIG.FIGBACK_ATTR);

			fig= Editor.STATE.CANVAS.circle(x, y, Editor.CONFIG.DECKEDIT_RADIUS).attr(Editor.CONFIG.DECKEDIT_ATTR);

			fig.mouseover(impl.getDeckEdit()._mouseover);
			fig.mouseout(impl.getDeckEdit()._mouseout);
			fig.click(impl.getDeckEdit().click);
			impl.getDeckEdit().toFront();
		},
		redraw: function () {
			var x = shape.getX() - Editor.CONFIG.DECKEDIT_DELTA;
			var y = shape.getY() + Editor.CONFIG.DECKEDIT_DELTA - 2*Editor.CONFIG.DECKEDIT_RADIUS + shape.getHeight();
			text.attr({
				x: shape.getX() + Editor.CONFIG.DECKEDIT_RADIUS - Editor.CONFIG.DECKEDIT_DELTA,
				y: shape.getY() + Editor.CONFIG.DECKEDIT_DELTA - Editor.CONFIG.DECKEDIT_RADIUS + shape.getHeight()
			});
			fig.translate(x-fig.getBBox().x, y-fig.getBBox().y);
			back.translate(x-back.getBBox().x, y-back.getBBox().y);
			impl.getDeckEdit().toFront();
		},
		_mouseover: function() {
      if (!Editor.Util.isDragging()) {
				detailText = Editor.STATE.CANVAS.text(0, 0, impl.getInfo());
				detailText.attr(Editor.CONFIG.DETAILTEXT_ATTR);
				detailText.translate(detailText.getBBox().x*-1,detailText.getBBox().y*-1);

				var x = shape.getX() + Editor.CONFIG.DETAIL_DELTA_X;
				var y = shape.getY() + shape.getHeight() + Editor.CONFIG.DETAIL_DELTA;
				var w = detailText.getBBox().width + 2*Editor.CONFIG.DETAIL_DELTA;
				if ((w+Editor.CONFIG.DETAIL_DELTA_X)<shape.getWidth()) {
					w = shape.getWidth()-Editor.CONFIG.DETAIL_DELTA_X;
				}
				var h=detailText.getBBox().height;

				detailText.translate(x + Editor.CONFIG.DETAIL_DELTA,y);

				detail = Editor.STATE.CANVAS.rect(x,y,w,h,0);
				detail.attr(Editor.CONFIG.DETAIL_ATTR);

				detailBack = Editor.STATE.CANVAS.rect(x,y,w,h,0);
				detailBack.attr(Editor.CONFIG.FIGBACK_ATTR);
				detailBack.attr(Editor.CONFIG.FIGBACK_ATTR_VISIBLE);

				detailBack.toFront();
				detail.toFront();
				detailText.toFront();
				impl.getDeckEdit().mouseover();
			}
		},
		_mouseout: function () {
			if (detailText) {
				detailText.remove();
				delete detailText;
				detail.remove();
				delete detail;
				detailBack.remove();
				delete detailBack;
				impl.getDeckEdit().mouseout();
			}
		},
		mouseover: function () {
			if (!Editor.Util.isDragging()) {
				text.attr(Editor.CONFIG.DECKEDIT_ATTR_TEXTSYMBOL_VISIBLE);
				fig.attr(Editor.CONFIG.DECKEDIT_ATTR_VISIBLE);
				back.attr(Editor.CONFIG.FIGBACK_ATTR_VISIBLE);
				impl.getDeckEdit().toFront();
			}
		},
		mouseout: function () {
			if (!Editor.Util.isDragging()) {
				text.attr(Editor.CONFIG.DECKEDIT_ATTR_TEXTSYMBOL);
				fig.attr(Editor.CONFIG.DECKEDIT_ATTR);
				back.attr(Editor.CONFIG.FIGBACK_ATTR);
			}
		},
		click: function () {
			impl.showEdit();
		},
		translate: function (deltaX, deltaY) {
			text.translate(deltaX, deltaY);
			fig.translate(deltaX, deltaY);
			back.translate(deltaX, deltaY);
		},
		toFront: function () {
			back.toFront();
			text.toFront();
			fig.toFront();
		}
	}
}
